package lt.egidijus.aatest.listener;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Semaphore;

import lt.egidijus.aatest.BrowserView;
import lt.egidijus.aatest.report.ReportGenerator;
import lt.egidijus.aatest.utils.Constants;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.junit.TestRunListener;
import org.eclipse.jdt.junit.model.ITestCaseElement;
import org.eclipse.jdt.junit.model.ITestRunSession;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.PlatformUI;



public class TestRunnerListener extends TestRunListener {
	
	private BrowserView bView = null;
	

	public TestRunnerListener(BrowserView bView) {
		this.bView = bView;
	}
	/**
	 * A test run session has been launched. The test tree is not available yet.
	 * <p>
	 * Important: The implementor of this method must not keep a reference to the session element
	 * after {@link #sessionFinished(ITestRunSession)} has finished.
	 * </p>
	 *
	 * @param session the session that has just been launched
	 * @since 3.6
	 */
	public void sessionLaunched(ITestRunSession session) {
	}
	
	/**
	 * A test run session has started. The test tree can be accessed through the session element.
	 * <p>
	 * Important: The implementor of this method must not keep a reference to the session element
	 * after {@link #sessionFinished(ITestRunSession)} has finished.
	 * </p>
	 *
	 * @param session the session that has just started.
	 */
	public void sessionStarted(ITestRunSession session) {
	}

	/**
	 * A test run session has finished. The test tree can be accessed through the session element.
	 *
	 * <p>
	 * Important: The implementor of this method must not keep the session element when the method is finished.
	 * </p>
	 *
	 * @param session the test
	 */
	public void sessionFinished(ITestRunSession session) {
		System.out.println("TestRunnerListener.sessionFinished()");
		
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.projectName);
		File projectFile = new File(project.getLocationURI());
		IProject project2 = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		File testFile = new File(project2.getLocationURI());
		IJavaProject projectJava = JavaCore.create(project2);	
		IFile coverageLog = projectJava.getProject().getFile(Constants.coverageResourcesFolder+"\\coverage.log");	
		try {
			coverageLog.refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e1) {
			e1.printStackTrace();
		}
		int a = 9999;
		while (!coverageLog.exists() || a ==0 ) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			a--;
		}
		
		if (coverageLog.exists()){
				System.out.println("Create report");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				ReportGenerator generator = new ReportGenerator(projectFile, testFile);
				try {
					generator.create();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Display display =  PlatformUI.getWorkbench().getDisplay();
				IFile ifold = projectJava.getProject().getFile(Constants.coverageResourcesFolder);

				try {
				   projectJava.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
				} catch (CoreException e) {
					e.printStackTrace();
				}
				final File index = new File(ifold.getLocationURI().toString(), "index.html");
		
				display.syncExec(
						  new Runnable() {
						    public void run(){
								bView.setLocationUrl(index.toString());
						    }
						  });
		}
	}

	/**
	 * A test case has started. The result can be accessed from the element.
	 * <p>
	 * Important: The implementor of this method must not keep a reference to the test case element
	 * after {@link #sessionFinished(ITestRunSession)} has finished.
	 * </p>
	 * @param testCaseElement the test that has started to run
	 */
	public void testCaseStarted(ITestCaseElement testCaseElement) {
	}

	/**
	 * A test case has ended. The result can be accessed from the element.
	 * <p>
	 * Important: The implementor of this method must not keep a reference to the test case element
	 * after {@link #sessionFinished(ITestRunSession)} has finished.
	 * </p>
	 *
	 * @param testCaseElement the test that has finished running
	 */
	public void testCaseFinished(ITestCaseElement testCaseElement) {

	}

}
